home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Movie Controller / MCPlayMovie / MCPlayMovie.c next >
Encoding:
Text File  |  1994-12-06  |  11.2 KB  |  523 lines  |  [TEXT/MPS ]

  1. /*
  2.   File:            MCPlayMovie.c
  3.   Contains:        Movie Playing Application Using Movie Controllers.
  4.   Written by:    John Wang / DTS
  5.   Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  6.   Change History (most recent first):
  7.   <3>         12/4/94        khs        changed the format of the file to the new look and feel
  8.   <2>         xx/xx/91    JW        Added palette support so that app restores color table
  9.                                   when changed by another app.
  10.   <1>         11/17/91    JW        1.0 Completed
  11.   To Do:
  12. */
  13.  
  14.  
  15. // INCLUDEs
  16. #include     <GestaltEqu.h>
  17. #include    <Palettes.h>
  18. #include    <SegLoad.h>
  19. #include    <Devices.h>
  20. #include    <ToolUtils.h>
  21. #include    <Errors.h>
  22.  
  23. #include    <Movies.h>
  24.  
  25.  
  26. // DEFINES
  27. #define Gestalttest        0xA1AD
  28. #define NoTrap            0xA89F
  29.  
  30. #define    appleID            128            
  31. #define    appleMenu        0
  32. #define    aboutMeCommand    1
  33.  
  34. #define    fileID            129
  35. #define openCommand        1
  36. #define    flattenCommand    2
  37. #define closeCommand    3
  38. #define    quitCommand     5
  39.  
  40. #define    aboutMeDLOG        128
  41. #define    okButton        1
  42.  
  43. #define    MAXWINDOWS        5
  44.  
  45.  
  46. // FUNCTION PROTOTYPES
  47. Movie GetMovieFromFile(void);
  48. OSErr PlayMovie(int index);
  49. short Flatten(int index);
  50. void ShowAboutMeDialog(void);
  51. void Init(void);
  52. void Finish(void);
  53. void DoCloseCommand(void);
  54. void DoCommand(long mResult);
  55. int PlayMovies(EventRecord* myEvent);
  56. void DoOpenCommand(void);
  57. void DoFlattenCommand(void);
  58.  
  59. // GLOBALS
  60. Boolean DoneFlag = false;
  61. MenuHandle mymenu0, mymenu1;
  62. Boolean playingMovie[MAXWINDOWS];
  63. Movie myMovie[MAXWINDOWS];
  64. WindowPtr myWindow[MAXWINDOWS];
  65. MovieController mcPlay[MAXWINDOWS];
  66. int startlocation;
  67. CTabHandle mycolors;
  68. PaletteHandle srcPalette;
  69.  
  70.  
  71. // FUNCTIONS
  72. /*------------------------------------------------------*/
  73. /*    GetMovieFromFile().                                    */
  74. /*------------------------------------------------------*/
  75.  
  76. Movie GetMovieFromFile(void)
  77. {
  78.     OSErr err;
  79.     StandardFileReply reply;
  80.     Point where =
  81.     {
  82.         200,  50
  83.     }
  84.     ;
  85.     SFTypeList types;
  86.     short movieResRefNum;
  87.     short actualResId;
  88.     Movie theMovie;
  89.  
  90.     types[0] = 'MooV';
  91.     StandardGetFilePreview(nil, 1, types, &reply);
  92.     if (!reply.sfGood)
  93.         return ((Movie)0);
  94.  
  95.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  96.     if (GetMoviesError())
  97.         return ((Movie)0);
  98.     if (err)
  99.         return ((Movie)0);
  100.     actualResId = DoTheRightThing;
  101.  
  102.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, NULL, newMovieActive, (Boolean *)0);
  103.     if (GetMoviesError())
  104.         return ((Movie)0);
  105.     if (err)
  106.         return ((Movie)0);
  107.  
  108.     err = CloseMovieFile(movieResRefNum);
  109.     if (GetMoviesError())
  110.         return ((Movie)0);
  111.     if (err)
  112.         return ((Movie)0);
  113.  
  114.     return (theMovie);
  115. }
  116.  
  117. /*------------------------------------------------------*/
  118. /*    PlayMovie().                                            */
  119. /*------------------------------------------------------*/
  120.  
  121. OSErr PlayMovie(int index)
  122. {
  123.     Rect movieBounds;
  124.  
  125.     GetMovieBox(myMovie[index], &movieBounds);
  126.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  127.     SetMovieBox(myMovie[index], &movieBounds);
  128.     OffsetRect(&movieBounds, startlocation, startlocation);
  129.  
  130.  
  131.  
  132.     myWindow[index] = NewCWindow(0L, &movieBounds, "\pMovie!", 1, 0, (WindowPtr) - 1, false, 0L);
  133.     SetPalette((WindowPtr)myWindow[index], srcPalette, true);
  134.  
  135.     OffsetRect(&movieBounds, -startlocation, -startlocation);
  136.     startlocation += 50;
  137.     if (startlocation > 300)
  138.         startlocation = 50;
  139.  
  140.     SetMovieGWorld(myMovie[index], (CGrafPtr)myWindow[index], 0);
  141.  
  142.     if (GetMoviesError())
  143.         DebugStr("\pSetMovieGWorld error.");
  144.  
  145.     mcPlay[index] = NewMovieController(myMovie[index], &movieBounds, mcTopLeftMovie);
  146.     MCGetControllerBoundsRect(mcPlay[index], &movieBounds);
  147.     SizeWindow(myWindow[index], movieBounds.right - movieBounds.left, movieBounds.bottom - movieBounds.top, true);
  148.     /*    Uncomment these lines if you want to pre load the movie into ram.
  149.       Don't forget to increase the partition size when recompiling.
  150.       GotoBeginningOfMovie(myMovie[index]);
  151.       if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  152.       GetMovieDuration(myMovie[index]),
  153.       0) != noErr)
  154.       DebugStr("\PNot enough memory to load movie into ram.");
  155.     */
  156.  
  157.     return noErr;
  158. }
  159.  
  160. /*------------------------------------------------------*/
  161. /*    Flatten().                                            */
  162. /*------------------------------------------------------*/
  163.  
  164. short Flatten(int index)
  165. {
  166.     StandardFileReply reply;
  167.     OSErr theErr = noErr;
  168.  
  169.     StandardPutFile("\pName of flattened movie.", "\pUntitled", &reply);
  170.     if (!reply.sfGood)
  171.         return fnOpnErr;
  172.  
  173.     theErr = GetMoviesError();
  174.     if (theErr != noErr)
  175.         DebugStr("\pCall Before FlattenMovies failed.");
  176.  
  177.     FlattenMovie(myMovie[index], flattenAddMovieToDataFork, &reply.sfFile, 'JWJW', 0, createMovieFileDeleteCurFile, nil, nil);
  178.  
  179.     theErr = GetMoviesError();
  180.     if (theErr != noErr)
  181.         DebugStr("\pFlattenMovies failed.");
  182.  
  183.     return (theErr);
  184. }
  185.  
  186. /*------------------------------------------------------*/
  187. /*    ShowAboutMeDialog()                                    */
  188. /*------------------------------------------------------*/
  189.  
  190. void ShowAboutMeDialog(void)
  191. {
  192.     GrafPtr savePort;
  193.     DialogPtr theDialog;
  194.     short itemHit;
  195.  
  196.     GetPort(&savePort);
  197.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) - 1);
  198.     SetPort(theDialog);
  199.  
  200.     do
  201.     {
  202.         ModalDialog(nil, &itemHit);
  203.     } while (itemHit != okButton);
  204.  
  205.     CloseDialog(theDialog);
  206.  
  207.     SetPort(savePort);
  208.     return;
  209. }
  210.  
  211. /*------------------------------------------------------*/
  212. /*    Init().                                                */
  213. /*------------------------------------------------------*/
  214.  
  215. void Init(void)
  216. {
  217.     OSErr err;
  218.     int i;
  219.     long QDfeature, OSfeature;
  220.  
  221.     /*    Initialize Managaer.    */
  222.     InitGraf(&qd.thePort);
  223.     FlushEvents(everyEvent, 0);
  224.     InitWindows();
  225.     InitDialogs(nil);
  226.     InitCursor();
  227.  
  228.     /*    Set up menus.    */
  229.     mymenu0 = GetMenu(appleID);
  230.     AddResMenu(mymenu0, 'DRVR');
  231.     InsertMenu(mymenu0, 0);
  232.     mymenu1 = GetMenu(fileID);
  233.     InsertMenu(mymenu1, 0);
  234.     DrawMenuBar();
  235.  
  236.     /*    Set up variables.    */
  237.     startlocation = 50;
  238.     for (i = 0; i < MAXWINDOWS; i++)
  239.     {
  240.         playingMovie[i] = false;
  241.         myWindow[i] = nil;
  242.     }
  243.     mycolors = GetCTable(72);
  244.     if (mycolors == nil)
  245.         Debugger();
  246.     srcPalette = NewPalette(((**mycolors).ctSize) + 1, mycolors, pmTolerant, 0);
  247.  
  248.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  249.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap)))
  250.     {
  251.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  252.         if (err)
  253.             ExitToShell();
  254.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  255.         if (err)
  256.             ExitToShell();
  257.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  258.             ExitToShell();
  259.         err = Gestalt(gestaltQuickTime, &QDfeature);
  260.         if (err)
  261.             ExitToShell();
  262.     }
  263.     else
  264.         ExitToShell();
  265.  
  266.     /*    Open QuickTime last.    */
  267.     err = EnterMovies();
  268.     if (err)
  269.         ExitToShell();
  270. }
  271.  
  272. /*------------------------------------------------------*/
  273. /*    Finish().                                            */
  274. /*------------------------------------------------------*/
  275.  
  276. void Finish(void)
  277. {
  278.     int i;
  279.  
  280.     for (i = 0; i < MAXWINDOWS; i++)
  281.         if (playingMovie[i] == true)
  282.         {
  283.             CloseComponent(mcPlay[i]);
  284.             DisposeMovie(myMovie[i]);
  285.             DisposeWindow(myWindow[i]);
  286.             playingMovie[i] = false;
  287.         }
  288.  
  289.     ExitMovies();
  290.     ExitToShell();
  291. }
  292.  
  293. /*------------------------------------------------------*/
  294. /*    DoOpenCommand().                                        */
  295. /*------------------------------------------------------*/
  296.  
  297. void DoOpenCommand(void)
  298. {
  299.     int i,
  300.      useThisIndex;
  301.  
  302.     useThisIndex = -1;
  303.  
  304.     /*    Search for the first window that is nil.    */
  305.     for (i = MAXWINDOWS - 1; i >= 0; i--)
  306.         if (myWindow[i] == nil)
  307.             useThisIndex = i;
  308.  
  309.         /*    If index = -1, then it means that there are no windows avaiable.    */
  310.     if (useThisIndex != -1)
  311.     {
  312.         myMovie[useThisIndex] = GetMovieFromFile();
  313.         if (myMovie[useThisIndex] != 0)
  314.         {
  315.             PlayMovie(useThisIndex);
  316.             playingMovie[useThisIndex] = true;
  317.         }
  318.     }
  319. }
  320.  
  321. /*------------------------------------------------------*/
  322. /*    DoFlattenCommand().                                        */
  323. /*------------------------------------------------------*/
  324.  
  325. void DoFlattenCommand(void)
  326. {
  327.     int i;
  328.     WindowPtr myTempWindow;
  329.  
  330.     /*    Flatten movie that is currently selected.    */
  331.     myTempWindow = FrontWindow();
  332.     if (myTempWindow == nil)
  333.         return;
  334.     for (i = 0; i < MAXWINDOWS; i++)
  335.         if (myWindow[i] == myTempWindow)
  336.             Flatten(i);
  337. }
  338.  
  339. /*------------------------------------------------------*/
  340. /*    DoCloseCommand().                                        */
  341. /*------------------------------------------------------*/
  342.  
  343. void DoCloseCommand(void)
  344. {
  345.     int i;
  346.     WindowPtr myTempWindow;
  347.  
  348.     /*    Close selected window.    */
  349.     myTempWindow = FrontWindow();
  350.     if (myTempWindow == nil)
  351.         return;
  352.     for (i = 0; i < MAXWINDOWS; i++)
  353.         if (myWindow[i] == myTempWindow)
  354.         {
  355.             CloseComponent(mcPlay[i]);
  356.             DisposeMovie(myMovie[i]);
  357.             DisposeWindow(myTempWindow);
  358.             playingMovie[i] = false;
  359.             myWindow[i] = nil;
  360.         }
  361. }
  362.  
  363. /*------------------------------------------------------*/
  364. /*    DoCommand().                                        */
  365. /*------------------------------------------------------*/
  366.  
  367. void DoCommand(long mResult)
  368. {
  369.     int theMenu, theItem;
  370.     Str255 daName;
  371.     GrafPtr savePort;
  372.  
  373.     theItem = LoWord(mResult);
  374.     theMenu = HiWord(mResult);
  375.  
  376.     switch (theMenu)
  377.     {
  378.         case appleID:
  379.             if (theItem == aboutMeCommand)
  380.                 ShowAboutMeDialog();
  381.             else
  382.             {
  383.                 GetItem(mymenu0, theItem, daName);
  384.                 GetPort(&savePort);
  385.                 (void)OpenDeskAcc(daName);
  386.                 SetPort(savePort);
  387.             }
  388.             break;
  389.  
  390.         case fileID:
  391.             switch (theItem)
  392.             {
  393.                 case openCommand:
  394.                     DoOpenCommand();
  395.                     break;
  396.                 case flattenCommand:
  397.                     DoFlattenCommand();
  398.                     break;
  399.                 case closeCommand:
  400.                     DoCloseCommand();
  401.                     break;
  402.                 case quitCommand:
  403.                     DoneFlag = true;
  404.                     break;
  405.                 default:
  406.                     break;
  407.             }
  408.             break;
  409.     }
  410.     HiliteMenu(0);
  411.     return;
  412. }
  413.  
  414. /*------------------------------------------------------*/
  415. /*    PlayMovies().                                            */
  416. /*------------------------------------------------------*/
  417.  
  418. int PlayMovies(EventRecord* myEvent)
  419. {
  420.     int i;
  421.  
  422.     for (i = 0; i < MAXWINDOWS; i++)
  423.         if (playingMovie[i] == true)
  424.         {
  425.             if (MCIsPlayerEvent(mcPlay[i], myEvent))
  426.                 return (true);
  427.         }
  428.     return (false);
  429. }
  430.  
  431. /*------------------------------------------------------*/
  432. /*    main().                                                */
  433. /*------------------------------------------------------*/
  434.  
  435. main()
  436. {
  437.     int i;
  438.     char key;
  439.     Boolean track;
  440.     EventRecord myEvent;
  441.     WindowPtr whichWindow;
  442.     int yieldTime;
  443.     int ret;
  444.  
  445.     Init();
  446.     yieldTime = 0;
  447.     for (;;)
  448.     {
  449.  
  450.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  451.         if (DoneFlag)
  452.             Finish();
  453.  
  454.         ret = WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
  455.  
  456.         if (PlayMovies(&myEvent))
  457.             continue;
  458.  
  459.         if (ret)
  460.         {
  461.  
  462.             switch (myEvent.what)
  463.             {
  464.                 case mouseDown:
  465.                     switch (FindWindow(myEvent.where, &whichWindow))
  466.                     {
  467.                         case inSysWindow:
  468.                             SystemClick(&myEvent, whichWindow);
  469.                             break;
  470.                         case inMenuBar:
  471.                             DoCommand(MenuSelect(myEvent.where));
  472.                             break;
  473.                         case inContent:
  474.                             SelectWindow(whichWindow);
  475.                             break;
  476.                         case inDrag:
  477.                             DragWindow(whichWindow, myEvent.where, &qd.screenBits.bounds);
  478.                             break;
  479.                         case inGrow:
  480.                             break;
  481.                         case inGoAway:
  482.                             track = TrackGoAway(whichWindow, myEvent.where);
  483.                             if (track)
  484.                                 DoCloseCommand();
  485.                             break;
  486.                         case inZoomIn:
  487.                             break;
  488.                         case inZoomOut:
  489.                             break;
  490.                         default:
  491.                             break;
  492.                     }
  493.                     break;
  494.                 case keyDown:
  495.                 case autoKey:
  496.                     key = myEvent.message & charCodeMask;
  497.                     if (myEvent.modifiers & cmdKey)
  498.                         if (myEvent.what == keyDown)
  499.                             DoCommand(MenuKey(key));
  500.                     break;
  501.                 case updateEvt:
  502.                     for (i = 0; i < MAXWINDOWS; i++)
  503.                         if ((WindowPtr)myEvent.message == myWindow[i])
  504.                         {
  505.                             BeginUpdate((WindowPtr)myWindow[i]);
  506.                             EndUpdate((WindowPtr)myWindow[i]);
  507.                         }
  508.                     break;
  509.                 case diskEvt:
  510.                     break;
  511.                 case activateEvt:
  512.                     break;
  513.                 case app4Evt:
  514.                     break;
  515.                 default:
  516.                     break;
  517.             }
  518.         }
  519.     }
  520. }
  521.  
  522.  
  523.